<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/9/12
 * Time: 15:29
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Feedback extends Base {
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $map =array();
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            if(isset($keyword)&&$keyword!="")
            {
                $map['uuu.user_username|uuu.user_phone'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($feedback_status)&&$feedback_status!="")
            {
                $map['a.feedback_status'] = $feedback_status;
            }
            if(isset($feedback_type_id)&&$feedback_type_id!="")
            {
                $map['a.feedback_type_id'] = $feedback_type_id;
            }
            if(isset($feedback_end)&&$feedback_end!="")
            {
                $map['a.feedback_end'] = $feedback_end;
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['a.feedback_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['a.feedback_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['a.feedback_time'] = ['between time',[$start,$end]];
            }
            $count = Db::table('up_feedback')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.f_user_id','left')
                ->join('up_feedback_type uft','uft.feedback_type_id = a.feedback_type_id','left')
                ->where($map)
                ->count();
            $list = Db::table('up_feedback')->alias('a')
                ->where($map)
                ->join('up_feedback_type uft','uft.feedback_type_id = a.feedback_type_id','left')
                ->join('up_u_user uuu','uuu.user_id = a.f_user_id','left')
                ->field('uuu.user_username,uuu.user_phone,uft.feedback_name,a.*')
                ->order('feedback_time desc')
                ->page($page, $limit)
                ->select();//计算总页面

            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        $grade = Db::table('up_feedback_type')->select();
        $this->assign('grade',$grade);
        return $this->fetch();
    }

    public function reject(){
        if(Request::instance()->isPost()){
            $ids = input('param.id');
            $data['feedback_status'] = 2;
            $data['feedback_text'] = input('param.reject');
            $res = Db::table('up_feedback')->where('feedback_id', $ids)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '处理成功']);
            }else{
                return json(['code' => 100, 'msg' => '处理失败']);
            }
        }
        $id = input('param.feedback_id');
        $this->assign('id',$id);
        return $this->fetch();
    }
    
    /**
     * 意见反馈详情
     * @return mixed
     */
    public function feedbackDetail(){
        $feedback_id = input('feedback_id');
        $detail = Db::table('up_feedback')->alias('a')
            ->join('up_u_user', 'user_id = f_user_id', 'left')
            ->join('up_feedback_type uft', 'a.feedback_type_id = uft.feedback_type_id', 'left')
            ->where('feedback_id', $feedback_id)
            ->field('user_username, feedback_name, feedback_phone, feedback_content, feedback_time, 
            feedback_status, feedback_text, feedback_end, feedback_img')
            ->find();
        $img = trim($detail['feedback_img'], ',');
        $detail['feedback_img'] = explode(',', $img);
        $this->assign('res', $detail);
        return $this->fetch('feedback/feedbackdetail');
    }
}